package com.sanpu.kxe.business.card.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.bean.BankInfoInput;
import com.sanpu.kxe.business.card.contract.CardConfig;
import com.sanpu.kxe.business.card.contract.VerificationContract;
import com.sanpu.kxe.business.card.presenter.VerificationPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.material.MaterialEditText;
import com.sanpu.kxe.module.roundview.RoundTextView;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Description：验证码确认页面
 * Created by 薛勇军 on 2018/7/24.
 */

public class VerificationActivity extends BaseTitleActivity implements VerificationContract.View {

    @Bind(R.id.hint)
    TextView vHint;
    @Bind(R.id.verification_code)
    MaterialEditText vVerificationCode;
    @Bind(R.id.send_code)
    RoundTextView vSendCode;
    @Bind(R.id.next)
    RoundTextView vNext;

    private VerificationContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_verification;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("填写验证码");
        BankInfoInput infoInput = (BankInfoInput) getIntent().getSerializableExtra(CardConfig.INTENT_BANKINFO);
        if (infoInput != null) {
            String mPhone = infoInput.getRsdTel();
            if (!TextUtils.isEmpty(mPhone) && mPhone.length() > 10) {
                String temp = mPhone.substring(7, mPhone.length());
                vHint.setText("请输入手机尾号" + temp + "收到的短信验证码");
            }
        }

        mPresenter = new VerificationPresenter(this);
        mPresenter.initViewData(infoInput);
    }

    @Override
    protected void setListener() {
        super.setListener();
        RxView.clicks(vSendCode).throttleFirst(CommonConfig.SMS_SEC, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.sendCode();
                    }
                });
        vNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.onCommit(vVerificationCode.getText().toString());
            }
        });
    }

    @Override
    public void changeByStatus(boolean status) {
        if (status) {
            vSendCode.setEnabled(false);
            vSendCode.getDelegate().setBackgroundColor(
                    ContextCompat.getColor(mContext, R.color.c_cccccc));
            vSendCode.getDelegate().setBackgroundPressColor(
                    ContextCompat.getColor(mContext, R.color.c_cccccc));
        } else {
            vSendCode.setText("获取验证码");//数据发送完后设置为原来的文字
            vSendCode.setEnabled(true);
            vSendCode.getDelegate().setBackgroundColor(
                    ContextCompat.getColor(mContext, R.color.color_base));
            vSendCode.getDelegate().setBackgroundPressColor(
                    ContextCompat.getColor(mContext, R.color.base_press_color));
        }
    }

    @Override
    public void byoyomi(Long aLong) {
        vSendCode.setText(String.valueOf(aLong + "秒"));
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(VerificationContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
